package com.example.demo.common.com.example.demo;

import java.util.ArrayList;
import java.util.Iterator;

public class Demo08 {
    public static void main(String[] args) {
        ArrayList<String> strList = new ArrayList<>();
        strList.add("str1");
        strList.add("str2");
        strList.add("str3");

        ArrayList<Integer> intList = new ArrayList<>();
        intList.add(1);
        intList.add(2);
        intList.add(3);

        printList(strList);
        printList(intList);
        System.out.println("=================================");

        printList2(strList);
        printList2(intList);
        System.out.println("=================================");

        printList3(strList);
        printList3(intList);
    }

    // 未使用泛型
    public static void printList(ArrayList list) {
        for (Iterator iterator = list.iterator(); iterator.hasNext(); ) {
            Object object = (Object) iterator.next();
            System.out.println(object);
        }
    }

    // 使用泛型方法
    public static <T> void printList2(ArrayList<T> list) {
        Iterator<T> iterator = list.iterator();
        while (iterator.hasNext()) {
            T t = iterator.next();
            System.out.println(t);
        }
    }

    //占位符，也称为通配符。表示元素类型可以匹配任意类型
    public static void printList3(ArrayList<?> a) {
        for (Iterator<?> it = a.iterator(); it.hasNext(); ) {
            System.out.println(it.next());
        }
    }


}
